<?php
session_start();
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></meta>
<script type="text/javascript" src="../FileForInterface/popcalendar.js"></script>
</head>
<body>
<?php
include '../DataBase/ConnectDB.php';
include '../Security/CheckUser.php';

/**
 * キャンセルコンディションを検査する
 * @param $CheckTime
 * @param $ID
 * @return unknown_type
 */
function CheckCancel($CheckTime,$ID){
	$t = strtotime($CheckTime) - time() - MIN_HOUR*60*60;
	if($t >=0 ) return "<a href='../Admin/CancelOrder.php?id=".$ID."'>キャンセル   </a>";
	else return " ";
}

?>
<h1>予約検索</h1>
<form action='SearchOrder.php' method='POST'>
<table border="2" bordercolor="0000FF" cellspacing="1" cellpadding="7"
	width="100%">
	<tr>
		<td width="20%">アカウント名</td>
		<td width="40%"><input type="text" name="AccountName" size="37"></input></td>
		<td width="20%">会議室名</td>
		<td width="20%"><select name="RoomName">
		<?php
		mysql_select_db(MYSQL_DATABASE,$link);
		$result=mysql_query("SELECT * FROM ".MYSQL_TABLE_ROOMINFOR." WHERE roomstatus ='1'",$link);
		print ("<option value=''></option>");
		while($row=mysql_fetch_array($result)){
			print("<option value='".$row[0]."' > Room ".$row[0]."</option>");
		}
		?>
		</select></td>
	</tr>
	<tr>
		<td rowspan="2" width="20%">予約時間</td>
		<td width="40%">スタートタイム(YYYY/MM/DD)</td>
		<td colspan="2" width="40%">エンドタイム(YYYY/MM/DD)</td>
	</tr>
	<tr>
		<td><input type='text' id='startDay' name="startDay" size=10></input>
		<img src="../FileForInterface/show-calendar.gif" style="cursor: pointer"
			onclick="popUpCalendar(this, document.getElementById('startDay'), 'yyyy/mm/dd')" />
		<select name="startTimeH">
		<?php for($i=0; $i<24;$i++){print("<option value='".$i."' > ".$i."h</option>");}?>
		</select> : <select name="startTimeM">
			<option value=00>00</option>
			<option value=30>30</option>
		</select></td>
		<td colspan="2"><input type='text' id='endDay' name="endDay" size=10></input>
		<img src="../FileForInterface/show-calendar.gif" style="cursor: pointer"
			onclick="popUpCalendar(this, document.getElementById('endDay'), 'yyyy/mm/dd')" />
		<select name="stopTimeH">
		<?php for($i=0; $i<24;$i++){print("<option value='".$i."' > ".$i."h</option>");}?>
		</select> : <select name="stopTimeM">
			<option value=00>00</option>
			<option value=30>30</option>
		</select></td>
	</tr>
</table>
<input type="submit" name="submit" value="OK "></input> <input
	type="reset" value="RESET"></input></form>
		<?php
		if($_POST['submit']){
			//		if(0){
			$AccountName=$_POST['AccountName'];
			$RoomName=$_POST['RoomName'];
			mysql_select_db(MYSQL_DATABASE,$link);
			$check = true;
			if(!$link){
				print("<br> <script type='text/javascript'> alert('".SQL_ERR_MESS."');  window.location ='../User/BookRoom.php';</script>");
			}
//			予約を検索する
			$queryString = "SELECT * FROM ".MYSQL_TABLE_ORDER." WHERE orderstatus ='1'";
			if($RoomName) $queryString = $queryString."AND roomname = '".$RoomName."'";
			if($AccountName)  $queryString = $queryString." AND accountname like '%$AccountName%'";
			$LimitTime = time() + MIN_HOUR*60*60;
			$StartDay=explode("/",$_POST["startDay"]);
			$EndDay=explode("/",$_POST["endDay"]);
			if(($StartDay[0])&&($EndDay[0])){
				$StartTime=mktime($_POST["startTimeH"],$_POST["startTimeM"],0,$StartDay[1],$StartDay[2],$StartDay[0]);
				$EndTime=mktime($_POST["stopTimeH"],$_POST["stopTimeM"],0,$EndDay[1],$EndDay[2],$EndDay[0]);
				if($StartTime > $EndTime) $check = false;
				// 		        $queryString = $queryString." AND (('".date("Y-m-d G:i:s",$StartTime)."' <= starttime) AND ('".date("Y-m-d G:i:s",$EndTime)."' >= starttime) ";
				// 		        $queryString = $queryString." OR (('".date("Y-m-d G:i:s",$StartTime)."' <= endtime) AND ('".date("Y-m-d G:i:s",$EndTime)."' >= endtime) ";
				$queryString = $queryString."AND (( '".date("Y-m-d G:i:s",$StartTime)."' <= starttime AND '".date("Y-m-d G:i:s",$EndTime)."' > starttime )
 			OR ( '".date("Y-m-d G:i:s",$StartTime)."' >= starttime AND endtime > '".date("Y-m-d G:i:s",$StartTime)."' ))";
			}
			else if((!$StartDay[0])&&($EndDay[0])||($StartDay[0])&&(!$EndDay[0])){
				$check = false;
			}
			$queryString = $queryString." ORDER BY accountname,roomname,starttime DESC ";
			$result = mysql_query($queryString,$link);
			if(!$check){
				 	print("<br> <script type='text/javascript'> alert('".TIME_ERR_MESS."');  window.location ='../Admin/SearchOrder.php';</script>");
//				print("Error");
				exit();
			}
			if(mysql_num_rows($result)==0){
				//				print "Row : ".mysql_num_rows($result).
				print ("検索条件に応じて予約はありません！");
				exit();
			}
			echo "<h1>予約検索</h1>";
			?>
<table border="2" bordercolor="0000FF" cellspacing="1" cellpadding="7"
	width="100%">
	<tr>
		<td align="center" width="15%">アカウント名</td>
		<td align="center" width="15%">会議室名</td>
		<td align="center" width="27%">スタートタイム</td>
		<td align="center" width="27%">エンドタイム</td>
		<td align="center" width="15%">処理</td>
	</tr>
	<?php
		}
		?>
		<?php
		while($row=mysql_fetch_array($result)){
			$StartTime = strtotime($row[4]);
			if(!$StartTime) continue;
			?>
	<tr>
		<td align="center"><?php echo $row[1] ?></td>
		<td align="center"><?php echo $row[2] ?></td>
		<td align="center"><?php echo $row[4] ?></td>
		<td align="center"><?php echo $row[5] ?></td>
		<td align="center"><?php echo CheckCancel($row[4],$row[0]);?></td>
	</tr>
	<?php
		}
		?>
</table>



</body>
</html>
